package com.dom.domweb.modular.controller.sysmger;

import com.dom.common.constants.ConstantError;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.common.TabPager;
import com.dom.domweb.dao.entity.TOrganInfo;
import com.dom.domweb.dao.service.ITOrganInfoService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.util.ShiroUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 机构信息列管理类控制层
 * @Author xllei
 * @Date 2018/11/22 11:24 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/sys")
public class OrganController {
    @Autowired
    private ITOrganInfoService organInfoService;

    @RequestMapping("/getOrganList")
    public RspModel getOrganList(){
        ShiroUser user = ShiroUtil.getUser();

        List<TOrganInfo> organInfos = organInfoService.findOrganList(user.getMemCode());
        if(CollectionUtil.isEmpty(organInfos)){
            return new RspModel(ConstantError.ILLQUERYERROR);
        }
        return new RspModel(organInfos);
    }

    @RequestMapping("/getOrganPager")
    public TabPager getOrganPager(@RequestParam("limit")int limit, @RequestParam("page")int page,
                                  @RequestParam("organName") String organName,
                                  @RequestParam("stat")Integer stat){
        ShiroUser user = ShiroUtil.getUser();

        B049R request = new B049R();
        request.setPn(page);
        request.setPs(limit);
        request.setOrganName(organName);
        request.setStat(stat);

        B049A response = organInfoService.findOrganByPager(request, user);
        return new TabPager(response.getOrganInfoData(), response.getTotalNum());
    }

    @RequestMapping("/addOrganInfo")
    public RspModel addOrganInfo(@RequestParam("organName") String organName, @RequestParam("organLevel") Integer organLevel,
                                 @RequestParam("fatherOrganCode") Integer fatherOrganCode) {
        B040R request = new B040R();
        request.setOrganName(organName);
        request.setOrganLevel(organLevel);
        request.setFatherOrganCode(fatherOrganCode);

        ShiroUser user = ShiroUtil.getUser();
        request.setMemCode(user.getMemCode());

        B040A response = organInfoService.addOrganInfo(request);
        return new RspModel(response.getOrganCode());
    }

    @RequestMapping("/editOrganInfo")
    public RspModel editUserInfo(@RequestParam("id")Integer id, @RequestParam("organName")String organName,
                                 @RequestParam("organLevel")Integer organLevel, @RequestParam("fatherOrgan")Integer fatherOrgan,
                                 @RequestParam("stat")Integer stat){
        B042R request = new B042R();
        if(0 == id){
            return new RspModel("RSP090001", "请选择机构信息修改");
        }
        request.setOrganCode(id);
        request.setOrganName(organName);
        request.setFatherOrgan(fatherOrgan);
        request.setOrganLevel(organLevel);
        request.setStat(stat);

        B042A response = organInfoService.editOrganInfo(request);
        return new RspModel(response.getOrganId());
    }

    @RequestMapping("/delOrganInfo")
    public RspModel delUserInfo(@RequestParam("id") Integer id){
        if(0 == id){
            return new RspModel("RSP090001", "请选择机构信息修改");
        }

        B041R request = new B041R();
        request.setOrganId(id);

        B041A response = organInfoService.delOrganInfo(request);
        return new RspModel(response.getOrganId());
    }
}
